package com.example.mybatis.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//UserController AOP 实现
@Aspect  //定义切面
@Component
public class UserAspect {
    //定义切点
    //切点里是没有实现的，只是定义了拦截规则
    @Pointcut("execution(* com.example.mybatis.controller.UserController.*(..))")
    public void pointCut(){
    }

    //前置通知
    @Before("pointCut()")
    public void doBefore(){
        //业务代码
        System.out.println("执行前置通知");
    }

    //后置通知
    @After("pointCut()")
    public void doAfter(){
        //业务代码
        System.out.println("执行后置通知");
    }

    //return之前通知
    @AfterReturning("pointCut()")
    public void doAfterReturning(){
        System.out.println("执⾏AfterReturning⽅法");
    }
    //抛出异常之前通知
    @AfterThrowing("pointCut()")
    public void doAfterThrowing() {
        System.out.println("执⾏doAfterThrowing⽅法");
    }

    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        Object result = null;
        //执行前置业务代码
        System.out.println("执行前置业务代码");
        try {
            //执行拦截的方法
            result=joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        //执行后置业务代码
        System.out.println("执行后置业务代码");
        return result;
    }
}
